//
//  TopicSquareViewController.m
//  BaseProject
//
//  Created by 王洋 on 2020/12/10.
//  Copyright © 2020 王洋. All rights reserved.
//
#import "TopicSquareSegCell.h"

#import "RelatedTopicCell.h"


#import "XLPageViewController.h"
#import "TopicSquareSubListViewController.h"

#import "TopicSquareViewController.h"

@interface TopicSquareViewController ()<XLPageViewControllerDelegate,XLPageViewControllerDataSrouce,UITextFieldDelegate>

@property (nonatomic, strong) NSMutableArray *segDataArray;
@property (nonatomic, strong) NSMutableArray *segArray;

@property (nonatomic, strong) XLPageViewController *pageViewController;


@property (nonatomic, strong) NSMutableArray <ProClassificationModel *>*classificationDataArray;


@end

@implementation TopicSquareViewController

- (void)viewDidLoad {
    [super viewDidLoad];


    [self request_catData];
}

- (void)initView {
    
    self.title = @"话题广场";

    [self.view addSubview:self.pageViewController.view];
        
    [self.pageViewController.view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.left.right.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
    }];

}

#pragma mark -
#pragma mark TableViewDelegate&DataSource
- (UIViewController *)pageViewController:(XLPageViewController *)pageViewController viewControllerForIndex:(NSInteger)index {

    TopicSquareSubListViewController *vc = [[TopicSquareSubListViewController alloc] initWith:self.classificationDataArray[index].sysClassificationList];
    
    if (self.type == TopicSquare_selTopic) {
        
        vc.TopicSquareSubListBlock = ^(ProClassificationModel * _Nonnull model) {
            if (self.TopicSquareBlock) {
                self.TopicSquareBlock(model);
            }
            [self.navigationController popViewControllerAnimated:YES];
        };
    }
    
    return vc;
}

- (NSString *)pageViewController:(XLPageViewController *)pageViewController titleForIndex:(NSInteger)index {
    return self.classificationDataArray[index].parentName;
}

- (XLPageTitleCell *)pageViewController:(XLPageViewController *)pageViewController titleViewCellForItemAtIndex:(NSInteger)index {
    TopicSquareSegCell *cell = [pageViewController dequeueReusableTitleViewCellWithIdentifier:@"TopicSquareSegCellID" forIndex:index];

    cell.mainTitle.text = self.classificationDataArray[index].parentName;
    [cell.bgImage sd_setImageWithURL:[NSURL URLWithString:self.classificationDataArray[index].icon] placeholderImage:[UIImage imageNamed:@"placeholder"]];
    return cell;
}

- (NSInteger)pageViewControllerNumberOfPage {
    return self.classificationDataArray.count;
}

- (void)pageViewController:(XLPageViewController *)pageViewController didSelectedAtIndex:(NSInteger)index {


}

- (XLPageViewController *)pageViewController {
    
    if (!_pageViewController) {
        XLPageViewControllerConfig *config = [XLPageViewControllerConfig defaultConfig];
//        config.shadowLineColor = UIColorHex(0xff4242);

        config.titleNormalColor = UIColorHex(0xffffff);
        config.titleSelectedColor = UIColorHex(0xffffff);
        config.titleViewBackgroundColor = UIColorHex(0xffffff);
        config.titleSelectedFont = [UIFont boldSystemFontOfSize:15];
        config.titleNormalFont = [UIFont systemFontOfSize:14];
        config.titleViewAlignment = XLPageTitleViewAlignmentLeft;
        config.separatorLineHidden = YES;
        config.shadowLineAnimationType = XLPageShadowLineAnimationTypeNone;
        config.shadowLineWidth = CGFLOAT_MIN;
        config.titleWidth = 80;
        config.titleViewHeight = 60;
        config.titleSpace = 5;

        _pageViewController = [[XLPageViewController alloc] initWithConfig:config];
        _pageViewController.delegate = self;
        _pageViewController.dataSource = self;
        [self addChildViewController:_pageViewController];

        [_pageViewController registerNib:[UINib nibWithNibName:@"TopicSquareSegCell" bundle:nil] forTitleViewCellWithReuseIdentifier:@"TopicSquareSegCellID"];
    }
    return _pageViewController;
}

- (void)request_catData {
    
    NSMutableDictionary *dic = @{}.mutableCopy;

    [dic setValue:@"4" forKey:@"scType"];
    NSString *url = [NSString stringWithFormat:@"%@%@",TT_Global_Api_Domain,API_Category];
    
    [SVProgressHUD showWithStatus:@"正在加载"];
    
    WEAKSELF
    [TTHttpManager postRequestWithUrl:url TimeOut:10 PostData:dic FinishBlock:^(id  _Nonnull data) {
        
        [SVProgressHUD dismiss];
        if ([data isKindOfClass:[NSDictionary class]]) {

            //请求成功
            NSInteger code = [[data valueForKey:@"code"] integerValue];
            if (code == 10000) {
                NSDictionary *resultDic = [data valueForKey:@"data"];
                if (![resultDic isKindOfClass:[NSDictionary class]]) {
                    return;
                }
                NSArray *oneList = [resultDic valueForKey:@"sysClassificationList"];
                if ([oneList isKindOfClass:[NSArray class]]) {
                    
                    weakSelf.classificationDataArray = @[].mutableCopy;
                    for (int i = 0; i < oneList.count; i++) {
                        ProClassificationModel *oneModel = [ProClassificationModel modelWithDictionary:oneList[i]];
                        
                        NSArray *twoList = [oneList[i] valueForKey:@"sysClassificationList"];
                        oneModel.sysClassificationList = @[].mutableCopy;
                        if ([twoList isKindOfClass:[NSArray class]]) {
                            
                            for (int j = 0; j < twoList.count; j++) {
                                ProClassificationModel *twoModel = [ProClassificationModel modelWithDictionary:twoList[j]];
                                
                                //需要三级的时候打开
//                                NSArray *threeList = [twoList[j] valueForKey:@"sysClassificationList"];
//                                twoModel.sysClassificationList = @[].mutableCopy;
//                                if ([threeList isKindOfClass:[NSArray class]]) {
//                                    for (int k = 0; k < threeList.count; k++) {
//                                        ProClassificationModel *threeModel = [ProClassificationModel modelWithDictionary:threeList[k]];
//                                        [twoModel.sysClassificationList addObject:threeModel];
//                                    }
//                                }
                                [oneModel.sysClassificationList addObject:twoModel];
                            }
                        }
                        [weakSelf.classificationDataArray addObject:oneModel];
                    }

                    [weakSelf initView];
                    
                }
            }
        }
        
    } FailedBlock:^(NSError * _Nonnull error) {
        
        [SVProgressHUD dismiss];
    }];
}

@end
